package com.gf.snoopy.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gf.snoopy.constants.enumc.TemplateType;
import com.gf.snoopy.dto.DownloadFile;
import com.gf.snoopy.dto.TemplateDto;
import com.gf.snoopy.platform.exception.PokeErrorCode;
import com.gf.snoopy.platform.exception.PokeException;
import com.gf.snoopy.platform.result.PokeResult;
import com.gf.snoopy.service.TemplateService;
import com.gf.snoopy.util.AoDataPage;
import com.gf.snoopy.util.SnoopyUtil;
import com.gf.snoopy.util.page.Page;
import com.gf.snoopy.util.page.PagedList;

@Controller
@RequestMapping("/template")
public class TemplateController {
	
	@Resource
	private TemplateService templateService;

	/**
	 * 分页获取机构详情
	 * @param voteId
	 * @param aoData
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/showPage")
	@ResponseBody	
	public String getTemplatePage(String aoData) throws Exception{
		if(aoData==null){
			throw new PokeException(PokeErrorCode.INVALID_PARAM);
		}
		AoDataPage aoDataPage = SnoopyUtil.parseAoData(aoData);
		Page page = new Page();
		page.setOffset(aoDataPage.offset);
		page.setPageSize(aoDataPage.pageSize);
		TemplateDto dto = new TemplateDto();
		dto.setName(aoDataPage.search);
		PagedList<TemplateDto> companyDtos = templateService.getTemplatePage(dto, page);
		return PokeResult.getPokeResultSuccess(SnoopyUtil.constructPageResult(companyDtos));
	}
	
	@RequestMapping("/upload")
	@ResponseBody
	public String saveOrUpateProject(Integer templateType,
			@RequestParam MultipartFile[] myfiles) throws Exception{
		if(myfiles==null || myfiles.length==0){
			return PokeResult.getPokeResultFail(PokeErrorCode.INVALID_PARAM, "上传文件为空");
		}
		MultipartFile file = myfiles[0];
		templateService.saveByMulti(TemplateType.parse(templateType), file);
		return PokeResult.getPokeResultSuccess(SnoopyUtil.SUCCESS);
	}
	
	@RequestMapping("/downloadTemplate")
	public void downloadTemplate(Integer id,HttpServletResponse response) throws PokeException{
		DownloadFile downloadFile = templateService.getFileByTemplateId(id);
		doDownloadFile(downloadFile.getFileName(),downloadFile.getFileData(), response);
	}
	
	@RequestMapping("/generateFileByProjectId")
	public void generateFileByProjectId(Integer projectId,Integer templateId,HttpServletResponse response) throws PokeException{
		DownloadFile downloadFile = null;
		if(templateId==null){
			downloadFile = templateService.generateAllFileByProjectId(projectId);
		}else{
			downloadFile = templateService.generateFileByProjectId(projectId, templateId);
		}
		doDownloadFile(downloadFile.getFileName(),downloadFile.getFileData(), response);
	}
	
	@RequestMapping("/generateFileBySubProjectId")
	public void generateFileBySubProjectId(Integer subProjectId,Integer templateId,HttpServletResponse response) throws PokeException{
		DownloadFile downloadFile = templateService.generateFileBySubProjectId(subProjectId, templateId);
		doDownloadFile(downloadFile.getFileName(),downloadFile.getFileData(), response);
	}	
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public String deleteById(Integer id) throws Exception{
		templateService.deleteById(id);
		return PokeResult.getPokeResultSuccess(SnoopyUtil.SUCCESS);
	}
	
	@RequestMapping(value="/findAll")
	@ResponseBody	
	public String findAllTemplate() throws Exception{
		return PokeResult.getPokeResultSuccess(templateService.findAllTemplate());
	}
	
	private void doDownloadFile(String fileName, byte[] fileData,HttpServletResponse response) throws PokeException{
		try {
			response.reset();
			response.setContentType("application/octet-stream");
			response.setContentType("text/html; charset=utf-8");
			response.addHeader("Content-Disposition", "attachment; filename="
					+ java.net.URLEncoder.encode(fileName, "UTF-8"));
			OutputStream ops;
			ops = response.getOutputStream();
			ops.write(fileData);
			ops.flush();
			ops.close();
		} catch (IOException e) {
			throw new PokeException(PokeErrorCode.INVALID_PARAM,"文件操作出错");
		}
	}
}
